#include "qlager.h"
#include "qispratnicivnes.h"
#include "qispratnicikorekcija.h"
#include "qizleznifakturivnes.h"
#include "qizleznifakturikorekcija.h"
#include "qprofakturavnes.h"
#include "qprofakturakorekcija.h"
#include "qprofakturaWizard.h"
#include "qnalogvnes.h"
#include "qnalogkorekcija.h"

QLager::QLager(QWidget *parent)
	: QMyBaseWidget(parent)
    ,lagerLista(0)
{
	ui.setupUi(this);
    pressF4();
}

QLager::~QLager()
{

}

void QLager::refresh()
{
	pressF4();
}

void QLager::pressF4()
{
    closeAllForm();
    lagerLista = showMyWidget<QLagerWidget>(lagerLista);
    connect(lagerLista, SIGNAL(closeW()), this, SLOT(closeWLista()));
    connect(lagerLista, SIGNAL(sLagerToDokument(QString&, QString&, QString&, QString&)), this, SLOT(callretLagerToDokument(QString&,QString&, QString&, QString&)));

}
void QLager::closeAllForm()
{
    lagerLista = deleteMyWidget<QLagerWidget>(lagerLista);
}

void QLager::closeWLista()
{
    pressEscape();
}

void QLager::pressEscape()
{
    raise();
    setFocus();

	QString text = "";
	QString textName = "";
	QString textNameSaldo = "";
	QString magTip = "";
	QWidget *sour = getSourceWidget();
	if (sour)
	{
		callretLagerToDokument(text, textName, textNameSaldo, magTip);
	} 
	else
	{
		emit signCloseMyWidget();	
	}
    
}


void QLager::callretLagerToDokument(QString& text, QString& textName, QString& textNameSaldo, QString& magTip)
{
    QWidget *sour = getSourceWidget();


	if (qobject_cast<QNalogVnes*>(sour))
	{
		QNalogVnes *dest = qobject_cast<QNalogVnes*>(sour);
		dest->setArtikli(text, textName);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}
	if (qobject_cast<QNalogKorekcija*>(sour))
	{
		QNalogKorekcija *dest = qobject_cast<QNalogKorekcija*>(sour);
		dest->setArtikli(text, textName);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}
	
	
	if (qobject_cast<QIspratniciVnes*>(sour))
    {
        QIspratniciVnes *dest = qobject_cast<QIspratniciVnes*>(sour);
        dest->setArtikli(text, textName, magTip);
//        dest->setMagId(magTip);
        emit eupdateNanigator(dest->getCategoryWidget(), this);
        dest->setFocusLine2();
    }
    if (qobject_cast<QIspratniciKorekcija*>(sour))
    {
        QIspratniciKorekcija *dest = qobject_cast<QIspratniciKorekcija*>(sour);
        dest->setArtikli(text, textName, magTip);
//        dest->setMagId(magTip);
        emit eupdateNanigator(dest->getCategoryWidget(), this);
        dest->setFocusLine2();
    }
    if (qobject_cast<QIzlezniFakturiVnes*>(sour))
    {
        QIzlezniFakturiVnes *dest = qobject_cast<QIzlezniFakturiVnes*>(sour);
        dest->setArtikli(text, textName);
        dest->setSaldo(textNameSaldo);
        emit eupdateNanigator(dest->getCategoryWidget(), this);
        dest->setFocusLine2();
    }
    if (qobject_cast<QIzlezniFakturiKorekcija*>(sour))
    {
        QIzlezniFakturiKorekcija *dest = qobject_cast<QIzlezniFakturiKorekcija*>(sour);
        dest->setArtikli(text, textName);
        dest->setSaldo(textNameSaldo);
        emit eupdateNanigator(dest->getCategoryWidget(), this);
        dest->setFocusLine2();
    }
	if (qobject_cast<QProFakturiVnes*>(sour))
	{
		QProFakturiVnes *dest = qobject_cast<QProFakturiVnes*>(sour);
		dest->setArtikli(text, textName);
		dest->setSaldo(textNameSaldo);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}

	if (qobject_cast<QProFakturiKorekcija*>(sour))
	{
		QProFakturiKorekcija *dest = qobject_cast<QProFakturiKorekcija*>(sour);
		dest->setArtikli(text, textName);
		dest->setSaldo(textNameSaldo);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}
    if (qobject_cast<QProFakturiWizard*>(sour))
    {
        QProFakturiWizard *dest = qobject_cast<QProFakturiWizard*>(sour);
        dest->setArtikli(text, textName);
        dest->setSaldo(textNameSaldo);
        emit eupdateNanigator(dest->getCategoryWidget(), this);
        dest->setFocusLine2();
    }
}

void QLager::setInitSearchText(QString text)
{
    if (lagerLista)
    {
        lagerLista->setInitText(text);
    } 
}

QStandardItemModel* QLager::getModel()
{
	QStandardItemModel * modelExt;
	if (lagerLista)
	{
		modelExt = lagerLista->model;
	} 
	else
	{
		modelExt = NULL;
	}
	return modelExt;
}